#pragma once 
#include <iostream>
#include <memory>
#include <string>
#include <jsoncpp/json/json.h>
#include <sstream>

std::string Sep ="\r\n";

bool Encode(std::string &message) //给字段加长度，方便判断报文是否够长
{
    if(message.size()==0)
    {
        return false;
    }
    std::string package = std::to_string(message.size()) + Sep + message + Sep;
    message = package;
    return true;
}
bool Decode(std::string &package, std::string *content)/*判断报文是否够长*/
{
    auto pos = package.find(Sep);
    if(pos == std::string::npos)
    {
       // std::cout<<"报文不够长"<<std::endl;
        return false;
    }
    std::string tmplen = package.substr(0,pos);
    int len = std::stoi(tmplen);
    int full_len =len+2*Sep.size() + tmplen.size();
    if(package.size()< full_len)
    {
        std::cout<<"报文不够长"<<std::endl;
        return false;
    }
    *content = package.substr(pos+Sep.size(),len);
    package.erase(0,full_len); 
    return true;


}

class Request
{
    public:
    Request()
    :_x(0)
    ,_y(0)
    ,_oper(0)
    {

    }
    Request(int x,int y,char oper)
    :_x(x)
    ,_y(y)
    ,_oper(oper)
    {}
    ~Request()
    {
    }
    bool Serialize(std::string& out_string)
    {
    //     Json::Value root;
    //     root["x"] = _x;
    //     root["y"] = _y;
    //     root["oper"] = _oper;

    //     Json::StreamWriterBuilder wb;
    //     std::unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
    //     std::stringstream ss;
    //     w->write(root,&ss);
    //     out = ss.str();
    //     return true;
         Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;

        Json::StreamWriterBuilder wb;
        std::unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
        std::stringstream ss;
        w->write(root, &ss);
        out_string = ss.str();
        return true;
     }
    bool deserialize(std::string& in_string )
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(in_string, root);
        if (!parsingSuccessful)
        {
            //std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
            return false;
        }

        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();

        return true;


    }
    void Print()
    {
        std::cout << _x << std::endl;
        std::cout << _oper << std::endl;
        std::cout << _y << std::endl;
    }
    int X() const { return _x; }
    int Y() const { return _y; }
    char Oper() const { return _oper; }

    private:
    int _x;
    int _y;
    char _oper;
    
};
class Response
{
    public:
     Response() : _result(0), _code(0)
    {
    }
    Response(int result, int code) : _result(result), _code(code)
    {
    }
    ~Response()
    {
    }
    bool Serialize(std::string& out_string)
    {
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;

        Json::StreamWriterBuilder wg;
        std::unique_ptr<Json::StreamWriter>  w(wg.newStreamWriter());
        std::stringstream ss;
        w->write(root,&ss);
        out_string = ss.str();

        return true;
        // Json::Value root;
        // root["result"] = _result;
        // root["code"] = _code;

        // Json::StreamWriterBuilder wb;
        // std::unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
        // std::stringstream ss;
        // w->write(root, &ss);
        // out_string = ss.str();
        // return true;

    }
    bool deserialize(std::string& in_string)
    {
        Json::Value root;
        Json::Reader reader;

        bool parsesucess = reader.parse(in_string,root);
        if(!parsesucess)
        {
            std::cout<<"parseucess false"<<std::endl;
            return false;
        }

        _result = root["result"].asInt();
        _code = root["code"].asInt();
        return true;
        // Json::Value root;
        // Json::Reader reader;
        // bool parsingSuccessful = reader.parse(in_string, root);
        // if (!parsingSuccessful)
        // {
        //     //std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
        //     return false;
        // }

        // _result = root["result"].asInt();
        // _code = root["code"].asInt();

        // return true;

    }
    int Result() const { return _result; }
    int Code() const { return _code; }
    void SetResult(int res) { _result = res;}
    void SetCode(int c) {_code = c;}

    private:
    int _result;
    int _code;
};